package com.xxrl.shop.repository;

import com.xxrl.shop.domain.Merchant;
import com.xxrl.shop.domain.Order;
import com.xxrl.shop.domain.Product;
import com.xxrl.shop.domain.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;


/**
 * @author mis
 */
public interface OrderRepository extends JpaRepository<Order,Long> {

    /**
     * 根据用户查询订单
     * @param user 用户
     * @param pageable 分页
     * @return 分页后的订单
     */
    Page<Order> queryOrdersByUser(User user, Pageable pageable);

    /**
     * 根据商品查询订单
     *
     * @param product  商品
     * @param pageable 分页
     * @return 分页后的订单
     */
    Page<Order> queryOrdersByProduct(Product product, Pageable pageable);

    Page<Order> queryOrdersByProductAndCommentExists(Product product, Pageable pageable);

    Page<Order> queryOrdersByProductAndCreateTimeAfter(Product product, Date date, Pageable pageable);

    /**
     * 根据商家查询
     *
     * @param merchant 商家
     * @param pageable 分页
     * @return 分页后的订单
     */
    @Query("select o from Order o where o.product.merchant = ?1")
    Page<Order> queryOrdersByMerchant(Merchant merchant, Pageable pageable);
}
